package com.example.lawyer.factory;


import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;

import java.util.List;

/**
 * 坐标定位
 */
public class LocationTool {


    /***
     * 定位
     */
    public void Positioning(Context context)
    {
        LocationManager systemService = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        List<String> providers = systemService.getProviders(true);
        String available = null;
        if(providers.contains(systemService.GPS_PROVIDER))
        {
            available =  systemService.GPS_PROVIDER;
        }else if(providers.contains(systemService.NETWORK_PROVIDER))
        {
            available = systemService.NETWORK_PROVIDER;
        }else {
            Toast.makeText(context,"没有科用的位置管理器",Toast.LENGTH_LONG).show();
            return;
        }

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        {

            System.out.println("失败");
            return;
        }

        Location location = systemService.getLastKnownLocation(available);
        Log.i("AAA","维度:"+location.getLatitude()+"经度:"+location.getLongitude());
        location.getLongitude();
        location.getLatitude();
    }

}
